package com.urchin.avatarhero.questview;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

import com.urchin.avatarhero.R;
import com.urchin.avatarhero.util.SQLiteHandler;
import com.urchin.avatarhero.util.Utilities;
import com.urchin.avatarhero.util.cal.CalBlob;
import com.urchin.avatarhero.util.cal.CalendarHandler;

/**********************************************************************
AvatarHero - A task management RPG Android Application
Copyright (C) 2013  Peter Clay

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

public class AvailQuestDialog extends DialogFragment {
	ArrayAdapter<String> qadapter;
	Activity act;

	public AvailQuestDialog() {
		
	}
	
	// Returns a new instance of this class
	static AvailQuestDialog newInstance(int num, int c, int e, int i, ArrayAdapter<String> qa, Activity a) {
		AvailQuestDialog f = new AvailQuestDialog();

		// Supply num input as an argument.
	    Bundle args = new Bundle();
	    args.putInt("num", num);
	    args.putInt("calid", c);
	    args.putInt("eventid", e);
	    args.putInt("instanceid", i);
	    f.setArguments(args);
	    
	    f.qadapter = qa;
	    f.act = a;
		        
	    return f;
	}

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		
		// Use the inflater to set the dialog view
		builder
			.setTitle(R.string.quest_dialog_avail)
			.setView(inflater.inflate(R.layout.dialog_qavail, null))
			.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					moveToCurrent();
				}
			})
			.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					AvailQuestDialog.this.getDialog().cancel();					
				}
			});	
		
		return builder.create();
	}
	
	public void moveToCurrent() {
		int cid = this.getArguments().getInt("calid");
		int eid = this.getArguments().getInt("eventid");
		int iid = this.getArguments().getInt("instanceid");
		
		// Get the diff level and set it
		EditText editText = (EditText) (this.getDialog().findViewById(R.id.dq_diff));
		int level = 0;
		
		// Try to parse the string. If not an Integer, just set to zero
		try {
			level = Integer.parseInt(editText.getText().toString().trim());
		} catch(NumberFormatException e) {
			level = 0;			// Set to 0
		}
		
		if((level <= 10) && (level > 0)) { 	// If the difficulty level is between 1 and 10
			// Now we have to update the GUI list in place
			// Get the Calendar data
		     CalendarHandler calhandle = new CalendarHandler(AvailQuestDialog.this.act);
		     // Get the Event and Instance data
		     ArrayList<CalBlob> cdata = calhandle.buildCalData(calhandle.getEvents(), calhandle.getInstances());
		     Utilities util = new Utilities(act);
								
			// Remove it before we add it (Yeah, it's a bit weird, but we need to remove it from adapter while diff is still 0)
			for(CalBlob caldata : cdata) {
				if((caldata.getInstanceid() == iid) && (caldata.getEventid() == eid)) {		// Event and Instance match
					qadapter.remove(util.convertToStrPattern(caldata, "caldisp"));		// Clear the adapter
					qadapter.notifyDataSetChanged();
					break;
				}
			}
			
			// Add it to the database
			SQLiteHandler dbHandler = new SQLiteHandler(this.getDialog().getContext());
			dbHandler.opendb();
			dbHandler.insertQuest(cid, eid, iid, level, 0);		// Create the quest with the difficulty given
			dbHandler.closedb();
			Toast.makeText(act.getApplicationContext(),"Level: " + level,Toast.LENGTH_SHORT).show();   
		}
	}
}
